<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="80px" size="mini"
             @submit.native.prevent>
      <el-form-item label="考核主题" prop="checkStaffId">
        <el-input v-model="queryParams.checkStaffId" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:checkPerformance:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新建绩效考核
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkPerformanceList" @selection-change="handleSelectionChange" border>
      <el-table-column align="center" label="考核主题" prop="cycleTitle" width="150px;"/>
      <el-table-column align="center" label="开始时间" prop="startTime" width="100px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="结束时间" prop="endTime" width="100px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色" prop="jobName" width="80px;" />
      <el-table-column align="center" label="考核指标">
        <template slot-scope="scope">
          <el-table :data="scope.row.resultList" border>
            <el-table-column align="center" label="指标名称" prop="indicatorName" width="150px;"/>
            <el-table-column align="center" label="权重(%)" prop="weight" width="80px;"/>
            <el-table-column align="center" label="定性/定量指标" prop="indicatorType" width="120px;">
              <template slot-scope="scope">
                <span>{{ scope.row.indicatorType == 1 ? "定性" : "定量" }}</span>
              </template>
            </el-table-column>
            <el-table-column align="center" label="计量单位" prop="indicatorUnit" width="80px;"/>
            <el-table-column align="center" label="指标定义" prop="indicatorDefinition" show-overflow-tooltip
                             width="100px;"/>
            <el-table-column align="center" label="评分标准" prop="indicatorStandard" show-overflow-tooltip
                             />
            <el-table-column align="center" label="评分依据" prop="indicatorBasis" show-overflow-tooltip
                             width="200px;"/>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column align="center" label="总权重(%)" prop="sumWeight" width="100px;"/>
      <el-table-column align="center" label="绩效总分" prop="performanceScore" width="100px;"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="150px;">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:checkPerformance:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-hasPermi="['cld:checkPerformance:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改绩效考核对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="考核周期" prop="cycleId">
              <el-select v-model="form.cycleId" placeholder="请选择">
                <el-option v-for="item in cycleList" :key="item.cycleId" :label="item.cycleTitle"
                           :value="item.cycleId"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核模版" prop="templateId">
              <el-select v-model="form.templateId" placeholder="请选择" @change="handleTemplateChange">
                <el-option v-for="item in templateList" :key="item.templateId" :label="item.templateName"
                           :value="item.templateId"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">考核指标</el-divider>
        <el-table :data="form.resultList">
          <el-table-column align="center" label="指标名称" prop="indicatorName" width="150px;"/>
          <el-table-column align="center" label="权重(%)" prop="weight" width="80px;"/>
          <el-table-column align="center" label="定性/定量指标" prop="indicatorType" width="120px;">
            <template slot-scope="scope">
              <span>{{ scope.row.indicatorType == 1 ? "定性" : "定量" }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="计量单位" prop="indicatorUnit" width="80px;"/>
          <el-table-column align="center" label="直接上级评分" prop="upScore" width="100px;">
            <template slot-scope="scope">
              <el-input v-model="scope.row.upScore" type="number" @input="calculatUpScore"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="评分标准" prop="indicatorStandard" />
          <el-table-column align="center" label="评分依据" prop="indicatorBasis" width="200px;"/>
        </el-table>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="指标总权重：">
              {{ form.sumWeight }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="绩效总分：">
              {{ form.performanceScore }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择人员 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isStaffOpen" append-to-body title="选择人员"
               width="70%">
      <SelectedStaff v-if="isStaffOpen" :isProject="false" :params="{ staffLevels: form.checkRole, staffStatus: 2 }"
                     @childInput="handleSelectedStaff">
      </SelectedStaff>
    </el-dialog>
  </div>
</template>

<script>
import {
  addCheckPerformance,
  delCheckPerformance,
  getCheckPerformance,
  listCheckPerformance,
  updateCheckPerformance
} from "@/api/cld/checkPerformance";
import {getCheckTemplateList,} from "@/api/cld/checkTemplate";
import {getCheckCycleList,} from "@/api/cld/checkCycle";
import SelectedStaff from '@/views/cld/staff/selectedStaff';

export default {
  name: "CheckPerformance",
  components: {
    SelectedStaff
  },
  data() {
    return {
      // 考核周期List
      cycleList: [],
      // 考核模版List
      templateList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 绩效考核表格数据
      checkPerformanceList: [],
      // 考核对象
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cycleId: [
          {required: true, message: "请选择考核周期", trigger: "change"},
        ],
        templateId: [
          {required: true, message: "请选择考核模版", trigger: "change"},
        ],
        checkStaffId: [
          {required: true, message: "请选择考核员工", trigger: "change"},
        ],
      },
      // 选择员工
      isStaffOpen: false
    };
  },
  created() {
    this.getList();
    this.getCycleAndTemplateList();
  },
  methods: {
    /** 查询绩效考核列表 */
    getList() {
      this.loading = true;
      listCheckPerformance(this.queryParams).then(response => {
        this.checkPerformanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询考核周期和考核模版
    getCycleAndTemplateList() {
      getCheckCycleList().then(res => {
        this.cycleList = res.data;
      })
      getCheckTemplateList().then(res => {
        this.templateList = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        performanceId: null,
        cycleId: null,
        templateId: null,
        checkStaffId: null,
        performanceScore: null,
        performanceLevel: null,
        staffName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.performanceId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加绩效考核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const performanceId = row.performanceId || this.ids
      getCheckPerformance(performanceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改绩效考核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.performanceId != null) {
            updateCheckPerformance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckPerformance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const performanceIds = row.performanceId || this.ids;
      this.$modal.confirm('是否确认删除绩效考核编号为"' + performanceIds + '"的数据项？').then(function () {
        return delCheckPerformance(performanceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 考核模版选择回调 根据选择的考核模版显示对应指标
    handleTemplateChange(e) {
      let obj = this.templateList.find(i => {
        return i.templateId == e
      })
      this.form.sumWeight = obj.sumWeight;
      this.form.checkRole = obj.checkRole;
      this.form.cardinality = obj.jobCategory;
      this.form.resultList = obj.indicatorList;
    },
    // 计算总权重
    calculatUpScore() {
      let upScore = 0;
      this.form.resultList.forEach(element => {
        upScore += parseFloat(element.upScore) * (parseFloat(element.weight) / 100);
      });
      this.form.performanceScore = upScore;
    },
    // 选择员工
    handleAddStaff() {
      this.isStaffOpen = true;
    },
    // 选择回调
    handleSelectedStaff(e) {
      this.form.checkStaffId = e.staffId;
      this.form.staffName = e.staffName;
      this.form.projectId = e.projectId,
      this.form.projectPostName = e.projectPostName;
      this.isStaffOpen = false;
    }
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

::v-deep .el-table__cell{
  padding:0px;
}

::v-deep .cell{
  padding:0px;
}
</style>
